import http from '../utils/request';

// 角色API前缀
const ROLE_PREFIX = '[Role]';

/**
 * 角色相关API
 */
export const roleApi = {
  /**
   * 获取角色列表
   * @param {Object} params - 查询参数
   * @returns {Promise<any>} 角色列表
   */
  getRoles: (params?: { enable?: boolean }): Promise<any> => {
    return http.get('/api/role', params);
  },

  /**
   * 分页获取角色列表
   * @param {Object} params - 分页参数
   * @returns {Promise<any>} 分页角色列表
   */
  getRolesPagination: (params: { pageSize?: number; pageNo?: number; name?: string; enable?: boolean }): Promise<any> => {
    return http.get('/api/role/page', params);
  },

  /**
   * 获取角色详情
   * @param {number|string} id - 角色ID
   * @returns {Promise<any>} 角色详情
   */
  getRole: (id: number | string): Promise<any> => {
    return http.get(`/api/role/${id}`);
  },

  /**
   * 创建角色
   * @param {Object} data - 角色数据
   * @returns {Promise<any>} 创建结果
   */
  createRole: (data: { code: string; name: string; permissionIds?: number[]; enable?: boolean }): Promise<any> => {
    return http.post('/api/role', data, {
      showLoading: true,
      showSuccess: true,
      successMsg: '角色创建成功'
    });
  },

  /**
   * 更新角色
   * @param {number|string} id - 角色ID
   * @param {Object} data - 角色数据
   * @returns {Promise<any>} 更新结果
   */
  updateRole: (id: number | string, data: { name?: string; permissionIds?: number[]; enable?: boolean }): Promise<any> => {
    return http.put(`/api/role/${id}`, data, {
      showLoading: true,
      showSuccess: true,
      successMsg: '角色更新成功'
    });
  },

  /**
   * 删除角色
   * @param {number|string} id - 角色ID
   * @returns {Promise<any>} 删除结果
   */
  deleteRole: (id: number | string): Promise<any> => {
    return http.delete(`/api/role/${id}`, {}, {
      showLoading: true,
      showSuccess: true,
      successMsg: '角色删除成功'
    });
  },

  /**
   * 获取角色权限
   * @param {number|string} id - 角色ID
   * @returns {Promise<any>} 角色权限
   */
  getRolePermissions: (id: number | string): Promise<any> => {
    return http.get('/api/role/permissions', { id });
  },

  /**
   * 添加角色权限
   * @param {Object} data - 角色权限数据
   * @returns {Promise<any>} 添加结果
   */
  addRolePermissions: (data: { id: number; permissionIds: number[] }): Promise<any> => {
    return http.post('/api/role/permissions/add', data, {
      showLoading: true,
      showSuccess: true,
      successMsg: '权限分配成功'
    });
  },

  /**
   * 获取角色权限树
   * @returns {Promise<any>} 角色权限树
   */
  getRolePermissionsTree: (): Promise<any> => {
    return http.get('/api/role/permissions/tree');
  },

  /**
   * 给角色分配用户
   * @param {number|string} roleId - 角色ID
   * @param {Object} data - 用户数据
   * @returns {Promise<any>} 分配结果
   */
  addRoleUsers: (roleId: number | string, data: { userIds: number[] }): Promise<any> => {
    return http.put(`/api/role/users/add/${roleId}`, data, {
      showLoading: true,
      showSuccess: true,
      successMsg: '用户分配成功'
    });
  },

  /**
   * 取消角色用户分配
   * @param {number|string} roleId - 角色ID
   * @param {Object} data - 用户数据
   * @returns {Promise<any>} 取消分配结果
   */
  removeRoleUsers: (roleId: number | string, data: { userIds: number[] }): Promise<any> => {
    return http.put(`/api/role/users/remove/${roleId}`, data, {
      showLoading: true,
      showSuccess: true,
      successMsg: '用户移除成功'
    });
  },
  
  /**
   * 获取角色枚举
   * @returns {Promise<any>} 角色枚举列表
   */
  getRoleEnum: (): Promise<any> => {
    return http.get('/api/role/enum');
  }
}; 